'use strict';

// 经纬度选择
app.controller('latLngSelectCtrl', function ($controller, $scope, $uibModalInstance, $timeout, data) {
    // var uiMapInfoWindowCtrl = $controller('uiMapInfoWindowCtrl', {$scope: $scope});
    var vm = $scope.vm = this;
    var location = data.location;
    vm.flag = data.flag;
    $scope.myMap = null;
    vm.mapOptions = {zoom: 13};
    //选择多个点的时候的数据组
    var markers = [];
    //选择的经纬度
    vm.selectLngLat = '';
    vm.lng = '';
    vm.lat = '';
    var marker = null;
    vm.deviceInfoWindow = null;
    var icon = "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png";
    vm.addAutocomplete = function () {
        if (window.AMap) {
            $scope.myMap.setCenter(new AMap.LngLat(114.512149, 30.473072));
            AMap.plugin(['AMap.Autocomplete', 'AMap.PlaceSearch'], function () {
                var autoOptions = {
                    city: "", //城市，默认全国
                    input: "input_id"//使用联想输入的input的id
                };
                var autocomplete = new AMap.Autocomplete(autoOptions);
                AMap.event.addListener(autocomplete, "select", function (e) {
                    if (e.poi && e.poi.location) {
                        $scope.myMap.setZoom(15);
                        $scope.myMap.setCenter(e.poi.location);
                    }
                });
            });
            //拖动定位
            if (null != location
                && ISS.isNotEmpty(location.latitude)
                && ISS.isNotEmpty(location.longitude)) {
                $scope.myMap.setMapCenter_(location.latitude, location.longitude);
                $scope.myMap.setZoom(15);
            }
        }
    }

    vm.mapClick = function (e, param) {

        vm.marker = {};
        // vm.taskName = null;
        vm.marker.lng = param[0].lnglat.getLng();
        vm.marker.lat = param[0].lnglat.getLat();
        vm.lng = param[0].lnglat.getLng();
        vm.lat = param[0].lnglat.getLat();
        marker = this.addMarker(vm.marker.lng, vm.marker.lat);
        vm.selectLngLat = vm.marker.lng + ',' + vm.marker.lat;
        if (vm.flag === "multi") {
            angular.forEach(markers, function (m, i) {
                if (m.bemsObj.taskName === "" || m.bemsObj.taskName === undefined) {
                    m.setMap(null);
                    markers.splice(i, 1);
                }
            })
            markers.push(marker);
            vm.deviceInfoWindow.open($scope.myMap, marker);
        }

    }
    this.addMarker = function (lng, lat) {
        if (vm.flag === "single" && marker) {
            //如果地图上有标记，先清空标记
            marker.setMap(null);
        }
        //创建新的标记
        marker = $scope.myMap.newMapMarker_(lng, lat, null, icon, false, angular.copy(vm.marker));
        marker.on("click", function (event) {
            vm.markerClick(event);
        })
        return marker;
    }

    vm.saveMarker = function (data) {
        // var marker = {};
        angular.forEach(markers, function (m) {
            var poi = m.getPosition_();
            if (poi.lng() == vm.marker.lng && poi.lat() == vm.marker.lat) {
                m.bemsObj = data;
            }
        })
        vm.deviceInfoWindow.close($scope.myMap);
    }

    //删除标记
    vm.deleteMarker = function (data) {
        vm.deviceInfoWindow.close($scope.myMap);
        angular.forEach(markers, function (m, i) {
            var poi = m.getPosition_();
            if (poi.lng() == vm.marker.lng && poi.lat() == vm.marker.lat) {
                m.setMap(null);
                markers.splice(i, 1);
            }
        })
    }

    //设备标记点击显示详细信息
    vm.markerClick = function (event) {
        if (vm.flag === "single") {
            return;
        }
        var marker = event.target;

        vm.marker = marker.bemsObj;
        $scope.$apply();
        vm.deviceInfoWindow.open($scope.myMap, marker);
    }


    vm.cancel = function () {
        $uibModalInstance.dismiss('Canceled');
    }; // end cancel

    var getMarker = function () {
        angular.forEach(markers, function (m, i) {
            if (m.bemsObj.taskName === undefined || m.bemsObj.taskName === "") {
                return i;
            } else {
                return -1;
            }
        });
    }
    vm.save = function () {
        if (vm.flag === "single") {
            $uibModalInstance.close({
                lng: vm.lng,
                lat: vm.lat
            });
        } else {
            $uibModalInstance.close({
                obj: markers
            });
        }
    }; // end save


});

